﻿// ResultRecordView.cpp: 实现文件
//

#include "stdafx.h"
#include "DC-QC.h"
#include "ResultRecordView.h"
#include "CSelDateDlg.h"
#include "math.h"


// CResultRecordView
#define RESULTVIEW_CONTROLBAR_HEIGHT 50
#define  RESULTVIEW_BT_WIDTH		100

IMPLEMENT_DYNCREATE(CResultRecordView, CFormView)

CResultRecordView::CResultRecordView()
	: CFormView(IDD_CResultRecordView)
{
	m_nCurMode = 0;
	m_nVSType = 0;
	m_nHSType = 0;
	m_nTimeSpan = 24 * 60;
	for (int i = 0; i < 9; i++)
		m_rtCheck[i] = CRect(0, 0, 0, 0);
}

CResultRecordView::~CResultRecordView()
{
}

void CResultRecordView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_GridControl(pDX, IDC_GRID, m_Grid);
	DDX_Control(pDX, IDC_BTDATE, m_btDate);
	DDX_Control(pDX, IDC_BTNOW, m_btReturn);
	DDX_Control(pDX, IDC_BTGRID, m_btGrid);
	DDX_Control(pDX, IDC_BTTREND, m_btTrend);
	DDX_Control(pDX, IDC_BTTOP, m_btToBegin);
	DDX_Control(pDX, IDC_BTBOTTOM, m_btToEnd);
	DDX_Control(pDX, IDC_BTUP, m_btUp);
	DDX_Control(pDX, IDC_BTDOWN, m_btDown);
	DDX_Control(pDX, IDC_CMBVS, m_cmbVS);
	DDX_Control(pDX, IDC_CMBHS, m_cmbHS);
}

BEGIN_MESSAGE_MAP(CResultRecordView, CFormView)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_BTDATE, &CResultRecordView::OnBnClickedBtdate)
	ON_BN_CLICKED(IDC_BTNOW, &CResultRecordView::OnBnClickedBtnow)
	ON_BN_CLICKED(IDC_BTGRID, &CResultRecordView::OnBnClickedBtgrid)
	ON_BN_CLICKED(IDC_BTTREND, &CResultRecordView::OnBnClickedBttrend)
	ON_CBN_SELCHANGE(IDC_CMBVS, &CResultRecordView::OnCbnSelchangeCmbvs)
	ON_CBN_SELCHANGE(IDC_CMBHS, &CResultRecordView::OnCbnSelchangeCmbhs)
	ON_WM_LBUTTONUP()
	ON_BN_CLICKED(IDC_BTTOP, &CResultRecordView::OnBnClickedBttop)
	ON_BN_CLICKED(IDC_BTBOTTOM, &CResultRecordView::OnBnClickedBtbottom)
	ON_BN_CLICKED(IDC_BTUP, &CResultRecordView::OnBnClickedBtup)
	ON_BN_CLICKED(IDC_BTDOWN, &CResultRecordView::OnBnClickedBtdown)
	ON_NOTIFY(NM_CLICK, IDC_GRID, &CResultRecordView::OnGridClick)
END_MESSAGE_MAP()


// CResultRecordView 诊断

#ifdef _DEBUG
void CResultRecordView::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void CResultRecordView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG


// CResultRecordView 消息处理程序


BOOL CResultRecordView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
	// TODO: 在此添加专用代码和/或调用基类

	return CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}


int CResultRecordView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	CRect rect(0, 0, 10, 10);
	m_wndHisCurve.Create(NULL, NULL, WS_CHILD | WS_VISIBLE, rect, this, WND_HISCUVE_ID, NULL);

	return 0;
}

void	CResultRecordView::RelayOut()
{
	CRect rect;
	GetClientRect(rect);
	try
	{
		if (m_Grid.m_hWnd)
		{
			int nBtWidth=RESULTVIEW_BT_WIDTH*g_DPIScaleX;
			CRect rt = rect;
			rt.bottom = rect.Height() / 2;
			m_wndHisCurve.MoveWindow(rt);

			rt.top = rt.bottom+8;
			rt.bottom = rect.Height() / 2 + RESULTVIEW_CONTROLBAR_HEIGHT*g_DPIScaleY-8*g_DPIScaleY;
			rt.left = 10*g_DPIScaleX;
			rt.right = rt.left + nBtWidth;
			m_btDate.MoveWindow(rt);
			rt.left = rt.right + 20*g_DPIScaleX;
			rt.right = rt.left + nBtWidth;
			m_btReturn.MoveWindow(rt);

			rt.left = rt.right+ 20*g_DPIScaleX;
			rt.right = rt.left + nBtWidth;
			m_btGrid.MoveWindow(rt);
			rt.left = rt.right + 10*g_DPIScaleX;
			rt.right = rt.left + nBtWidth;
			m_btTrend.MoveWindow(rt);

			rt.left = rt.right + 10*g_DPIScaleX;
			rt.right = rt.left + nBtWidth;
			m_cmbVS.MoveWindow(rt);
			rt.left = rt.right + 20*g_DPIScaleX;
			rt.right = rt.left + nBtWidth;
			m_cmbHS.MoveWindow(rt);


			rt.left = rect.right - (nBtWidth/2+10*g_DPIScaleX)*4;
			rt.right = rt.left + nBtWidth/2;
			m_btToBegin.MoveWindow(rt);
			rt.left = rt.right + 10*g_DPIScaleX;
			rt.right = rt.left + nBtWidth / 2;
			m_btToEnd.MoveWindow(rt);
			rt.left = rt.right + 10*g_DPIScaleX;
			rt.right = rt.left + nBtWidth / 2;
			m_btUp.MoveWindow(rt);
			rt.left = rt.right + 10*g_DPIScaleX;
			rt.right = rt.left + nBtWidth / 2;
			m_btDown.MoveWindow(rt);

			rt.left = 2;
			rt.right = rect.right - 2;
			rt.top = rect.Height() / 2 + RESULTVIEW_CONTROLBAR_HEIGHT*g_DPIScaleY;
			rt.bottom = rect.bottom;
			m_Grid.MoveWindow(rt);
		}
	}
	catch (...)
	{
	}

}

void CResultRecordView::OnSize(UINT nType, int cx, int cy)
{
	CFormView::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
	CRect rect;
	GetClientRect(rect);
	try
	{
		if (m_Grid.m_hWnd)
		{
			CRect rt = rect;
			rt.bottom = rect.Height() / 2;
			m_wndHisCurve.MoveWindow(rt);

			rt.top = rt.bottom + 8;
			rt.bottom = rect.Height() / 2 + RESULTVIEW_CONTROLBAR_HEIGHT-8;
			rt.left = 10;
			rt.right = rt.left + RESULTVIEW_BT_WIDTH;
			m_btDate.MoveWindow(rt);
			rt.left = rt.right + 20;
			rt.right = rt.left + RESULTVIEW_BT_WIDTH;
			m_btReturn.MoveWindow(rt);
			rt.left = rt.right + 20;
			rt.right = rt.left + RESULTVIEW_BT_WIDTH;
			m_cmbVS.MoveWindow(rt);
			rt.left = rt.right + 20;
			rt.right = rt.left + RESULTVIEW_BT_WIDTH;
			m_cmbHS.MoveWindow(rt);

			rt.left = rect.Width() / 2 - RESULTVIEW_BT_WIDTH - 10;
			rt.right = rt.left + RESULTVIEW_BT_WIDTH;
			m_btGrid.MoveWindow(rt);
			rt.left = rt.right + 20;
			rt.right = rt.left + RESULTVIEW_BT_WIDTH;
			m_btTrend.MoveWindow(rt);

			rt.left = rect.right - (RESULTVIEW_BT_WIDTH / 2 + 10) * 4;
			rt.right = rt.left + RESULTVIEW_BT_WIDTH / 2;
			m_btToBegin.MoveWindow(rt);
			rt.left = rt.right + 10;
			rt.right = rt.left + RESULTVIEW_BT_WIDTH / 2;
			m_btToEnd.MoveWindow(rt);
			rt.left = rt.right + 10;
			rt.right = rt.left + RESULTVIEW_BT_WIDTH / 2;
			m_btUp.MoveWindow(rt);
			rt.left = rt.right + 10;
			rt.right = rt.left + RESULTVIEW_BT_WIDTH / 2;
			m_btDown.MoveWindow(rt);
			
			rt.left = 2;
			rt.right = rect.right - 2;
			rt.top = rect.Height() / 2 + RESULTVIEW_CONTROLBAR_HEIGHT;
			rt.bottom = rect.bottom;
			m_Grid.MoveWindow(rt);
		}
	}
	catch (...)
	{
	}
}


void CResultRecordView::OnPaint()
{
	CPaintDC dc(this); // device context for painting
					   // TODO: 在此处添加消息处理程序代码
					   // 不为绘图消息调用 CFormView::OnPaint()
	CRect rect;
	GetClientRect(&rect);

	CBitmap ClientScreen, *pOldBitmap;
	CRect UpdateRect(rect);
	CDC MDC;
	MDC.CreateCompatibleDC(&dc);
	ClientScreen.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
	pOldBitmap = MDC.SelectObject(&ClientScreen);
	MDC.FillSolidRect(UpdateRect, RGB(255, 255, 255));
	Draw(&MDC, rect);
	dc.BitBlt(UpdateRect.left, UpdateRect.top,
		UpdateRect.right - UpdateRect.left,
		UpdateRect.bottom - UpdateRect.top,
		&MDC, UpdateRect.left, UpdateRect.top, SRCCOPY);
	MDC.SelectObject(pOldBitmap);

	ClientScreen.DeleteObject();
	MDC.DeleteDC();
}

void CResultRecordView::Draw(CDC* pDC, CRect rt)
{
	try
	{
		DrawInBorder(pDC, rt);
		rt.DeflateRect(1, 1, 1, 1);
		BxGradientFill(pDC, rt, BACKCOLOR1, BACKCOLOR2, 1);

		if (m_nCurMode == 1)
		{
			rt.top = rt.Height() / 2+ RESULTVIEW_CONTROLBAR_HEIGHT*g_DPIScaleY;
			DrawInBorder(pDC, rt);
			CRect rtC = rt;
			rtC.right = rtC.left + 120*g_DPIScaleX;
			rtC.DeflateRect(10*g_DPIScaleX, 10*g_DPIScaleY, 10*g_DPIScaleX, 10*g_DPIScaleY);
			CFont* pF = (CFont*)pDC->SelectObject(&gl.m_pSmallFont); //保存当前字体
			DrawGraphControl(rtC, pDC);
			rt.left = rtC.right;
			rt.DeflateRect(60*g_DPIScaleX, 10*g_DPIScaleY, 10*g_DPIScaleX, 30*g_DPIScaleY);
			m_rtGraph = rt;
			DrawFrame(rt, pDC);
			DrawCurve(rt, pDC);
			pDC->SelectObject(pF);		//恢复以前的字体
		}
	}
	catch (...)
	{
	}
}

void	CResultRecordView::DrawGraphControl(CRect rt, CDC *pDC)
{
	CSize s = m_dibChecked.GetDimensions();
	s.cx /= 2;

	pDC->FillSolidRect(rt, RGB(155, 155, 155));
	pDC->SetBkMode(TRANSPARENT);
	float  dCellH = rt.Height() / 9;
	CRect rtCell = rt;
	CString strTemp;
	for (int i = 0; i < 9; i++)
	{
		rtCell.top = rt.top + i * dCellH;
		rtCell.bottom= rt.top + (i+1) * dCellH;
		CRect rtSCell = rtCell;
		rtSCell.DeflateRect(2, 2, 2, 2);
		switch (i)
		{
		case 0:
			strTemp = _T("TG");
			break;
		case 1:
			strTemp = _T("C1");
			break;
		case 2:
			strTemp = _T("C2");
			break;
		case 3:
			strTemp = _T("C3");
			break;
		case 4:
			strTemp = _T("iC4");
			break;
		case 5:
			strTemp = _T("nC4");
			break;
		case 6:
			strTemp = _T("iC5");
			break;
		case 7:
			strTemp = _T("nC5");
			break;
		case 8:
			strTemp = _T("CO2");
			break;
		}
		pDC->SetTextColor(gl.gcMachine.nComColor[i]);
		pDC->DrawText(strTemp, rtSCell, DT_LEFT | DT_VCENTER | DT_SINGLELINE);

		CPoint pt1 = CPoint(rtSCell.left+ rtSCell.Width()*2/3+10, rtSCell.top);
		CPoint dp;
		dp.x = 0;
		if (gl.gcMachine.nComShow[i] == 1)
			dp.x = s.cx;
		dp.y = 0;
		m_dibChecked.Draw(pDC, pt1, CSize(s.cx*2/3, s.cy * 2 / 3), dp, s);
		m_rtCheck[i] = CRect(pt1, CSize(s.cx * 2 / 3, s.cy * 2 / 3));

		rtSCell.left = rtCell.left + rtCell.Width() / 3-6;
		rtSCell.top = rtSCell.top + 5;
		rtSCell.bottom = rtSCell.bottom - 5;
		rtSCell.right = rtSCell.left + rtCell.Width() / 3 + 8;
		pDC->FillSolidRect(rtSCell, gl.gcMachine.nComColor[i]);
	}
}

void	CResultRecordView::DrawFrame(CRect rt, CDC *pDC)
{
	CString strTemp;
	pDC->FillSolidRect(rt, RGB(155, 155, 155));
	int	m_left = rt.left;
	int	m_top = rt.top;
	int	m_right = rt.right;
	int	m_bottom = rt.bottom;
	//创建画笔对象
	CPen pPenBorder(PS_SOLID, 1, RGB(255, 255, 255));
	CPen pPenG(PS_DOT, 1, RGB(5, 85, 5));
	pDC->SetTextColor(RGB(255, 255, 255));
	pDC->SetBkMode(TRANSPARENT);

	CPen* pOldPen = pDC->SelectObject(&pPenBorder);

	// 绘制X轴刻度
	int nXScale = 60;
	int nXSpace = 20;
	switch (m_nHSType)
	{
	case 0:
		nXScale = 24 * 60;
		nXSpace = 30;
		break;
	case 1:
		nXScale = 12 * 60;
		nXSpace = 30;
		break;
	case 2:
		nXScale = 6 * 60;
		nXSpace = 30;
		break;
	case 3:
		nXScale = 2 * 60;
		nXSpace = 15;
		break;
	case 4:
		nXScale = 1 * 60;
		nXSpace = 5;
		break;
	}
	m_nXScale = nXScale;
	int nTemp;
	CRect rect1(0, 0, 0, 0);
	for (int i = 0; i <= nXScale; i=i+ nXSpace)
	{
		nTemp = (m_right - m_left)*i / nXScale + m_left;
		pDC->MoveTo(nTemp, m_bottom);
		if (i %(2* nXSpace) == 0)
		{
			pDC->SelectObject(&pPenBorder);
			pDC->LineTo(nTemp, m_top);
			pDC->SelectObject(&pPenG);
		}
		else
			pDC->LineTo(nTemp, m_bottom - 4);
		if (i % (2 * nXSpace) == 0)
		{
			CTime tTips = CTime(m_tStartTime.GetTime()+i*60);
			if(i%60==0)
				strTemp.Format(_T("%s"), tTips.Format(_T("%H")));
			else
				strTemp.Format(_T("%s"), tTips.Format(_T("%H:%M")));
			CRect rtA = CRect(CPoint(nTemp - 30*g_DPIScaleX, m_bottom), CSize(60*g_DPIScaleX, 20*g_DPIScaleY));
			pDC->DrawText(strTemp, rtA, DT_CENTER | DT_BOTTOM | DT_SINGLELINE);
		}
	}
	pDC->SelectObject(&pPenBorder);
	// 绘制 Y轴网格
	pDC->MoveTo(rt.TopLeft());
	pDC->LineTo(m_left, m_bottom);
	pDC->LineTo(rt.BottomRight());

	int nVTips = 3;
	switch (m_nVSType)
	{
	case 0:
		nVTips = 4;
		break;
	case 1:
		nVTips = 5;
		break;
	case 2:
		nVTips = 4;
		break;
	case 3:
		nVTips = 3;
		break;
	}
	m_nVLogNums = nVTips;
	for (int i = 0; i <= nVTips; i++)
	{
		nTemp = -(m_bottom - m_top)*i / nVTips + m_bottom;
		switch (m_nVSType)
		{
		case 0:
			switch (i)
			{
			case 0:
				strTemp = _T("0.0001");
				m_dVMinVal = 0.0001;
				break;
			case 1:
				strTemp = _T("0.001");
				break;
			case 2:
				strTemp = _T("0.01");
				break;
			case 3:
				strTemp = _T("0.1");
				break;
			case 4:
				strTemp = _T("1%");
				m_dVMaxVal = 1;
				break;
			}
			break;
		case 1:
			switch (i)
			{
			case 0:
				strTemp = _T("0.001");
				m_dVMinVal = 0.001;
				break;
			case 1:
				strTemp = _T("0.01");
				break;
			case 2:
				strTemp = _T("0.1");
				break;
			case 3:
				strTemp = _T("1");
				break;
			case 4:
				strTemp = _T("10");
				break;
			case 5:
				strTemp = _T("100%");
				m_dVMaxVal = 100;
				break;
			}
			break;
		case 2:
			switch (i)
			{
			case 0:
				strTemp = _T("0.001");
				m_dVMinVal = 0.001;
				break;
			case 1:
				strTemp = _T("0.01");
				break;
			case 2:
				strTemp = _T("0.1");
				break;
			case 3:
				strTemp = _T("1");
				break;
			case 4:
				strTemp = _T("10%");
				m_dVMaxVal = 10;
				break;
			}
			break;
			break;
		case 3:
			switch (i)
			{
			case 0:
				strTemp = _T("0.001");
				m_dVMinVal = 0.001;
				break;
			case 1:
				strTemp = _T("0.01");
				break;
			case 2:
				strTemp = _T("0.1");
				break;
			case 3:
				strTemp = _T("1%");
				m_dVMaxVal = 1;
				break;
			}
			break;
			break;
		}
		pDC->SelectObject(&pPenBorder);
		pDC->MoveTo(m_left, nTemp);
		pDC->LineTo(m_right, nTemp);
		CRect rtA=CRect(CPoint(m_left - 60*g_DPIScaleX, nTemp - 20*g_DPIScaleY),CSize(58*g_DPIScaleX,40*g_DPIScaleY));
		if (i == 0)
			pDC->DrawText(strTemp, rtA, DT_RIGHT | DT_TOP | DT_SINGLELINE);
		else if (i == nVTips)
			pDC->DrawText(strTemp, rtA, DT_RIGHT | DT_BOTTOM | DT_SINGLELINE);
		else
			pDC->DrawText(strTemp, rtA, DT_RIGHT | DT_VCENTER | DT_SINGLELINE);


		pDC->SelectObject(&pPenG);
		if (i != nVTips)
		{
			for (int k = 2; k <= 9; k++)
			{
				pDC->MoveTo(m_left, nTemp - (m_bottom - m_top) / nVTips * log10(k*1.0));
				pDC->LineTo(m_right, nTemp - (m_bottom - m_top) / nVTips * log10(k*1.0));
			}
		}
	}
	pDC->SetTextColor(RGB(255,0,0));
	strTemp.Format(_T("%s"), m_tStartTime.Format(_T("%m-%d %H:%M")));
	CRect rtRQ = CRect(CPoint(m_left + 2, m_top + 2), CSize(120*g_DPIScaleX, 20*g_DPIScaleY));
	pDC->FillSolidRect(rtRQ, RGB(226, 226, 226));
	pDC->DrawText(strTemp, rtRQ, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	pDC->SelectObject(pOldPen);
	pOldPen->DeleteObject();
}

void	CResultRecordView::DrawCurve(CRect rt, CDC *pDC)
{
	int	m_left = rt.left;
	int	m_top = rt.top;
	int	m_right = rt.right;
	int	m_bottom = rt.bottom;

	int nCount = arrChromResult.GetCount();
	long lStartTime = m_tStartTime.GetTime();
	CPoint pt;
	for (int i = 0; i < 9; i++)
	{
		if (gl.gcMachine.nComShow[i] == 0) continue;
		CPen pen(PS_SOLID, 1, gl.gcMachine.nComColor[i]);
		CPen* pOldPen = pDC->SelectObject(&pen);
		bool bStart = TRUE;
		for (int k = 0; k < nCount; k++)
		{
			CHROM_RESULT cr = arrChromResult.GetAt(k);
			if (cr.lTime< lStartTime || cr.lTime>lStartTime + m_nTimeSpan*60 ) continue;
			pt.x=(m_right - m_left)*(cr.lTime- lStartTime) / (m_nXScale*60) + m_left;
			float dValue = m_dVMinVal;
			switch (i)
			{
			case 0:
				dValue = cr.dTG;
				break;
			case 1:
				dValue = cr.dC1;
				break;
			case 2:
				dValue = cr.dC2;
				break;
			case 3:
				dValue = cr.dC3;
				break;
			case 4:
				dValue = cr.dIC4;
				break;
			case 5:
				dValue = cr.dNC4;
				break;
			case 6:
				dValue = cr.dIC5;
				break;
			case 7:
				dValue = cr.dNC5;
				break;
			case 8:
				dValue = cr.dCO2;
				break;
			}
			if (dValue < m_dVMinVal) dValue = m_dVMinVal;
			if (dValue > m_dVMaxVal) dValue = m_dVMaxVal;

			float dLog = log10(dValue*1.0);
			pt.y = m_bottom - (m_bottom - m_top) / m_nVLogNums * (dLog -log10(m_dVMinVal));
			if (bStart)
			{
				pDC->MoveTo(pt);
				bStart = FALSE;
			}
			else
				pDC->LineTo(pt);
		}
		pDC->SelectObject(pOldPen);
	}
}

void CResultRecordView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	// TODO: 在此添加专用代码和/或调用基类
	InitGrid();
	m_cmbVS.ShowWindow(SW_HIDE);
	m_cmbHS.ShowWindow(SW_HIDE);
	m_cmbVS.SetCurSel(m_nVSType);
	m_cmbHS.SetCurSel(m_nHSType);
	CWnd *pWnd = GetTopWindow();
	while (NULL != pWnd)
	{
		pWnd->SetFont(&gl.m_pSmallFont);
		pWnd = pWnd->GetNextWindow();
	}
	m_btGrid.SetIcon(IDI_REPORT, NULL);
	m_btTrend.SetIcon(IDI_TREND, NULL);
	m_dibChecked.LoadResource(IDB_CHECKSELECTED);
}

void CResultRecordView::InitGrid()
{
	//标定表格
	CString strTemp;
	m_Grid.SetFixedRowCount(1);
	m_Grid.SetFixedColumnCount(1);

	m_Grid.SetRowCount(100);
	m_Grid.SetColumnCount(13);
	m_Grid.SetItemText(0, 0, TEXT("时间"));		//
	m_Grid.SetItemText(0, 1, TEXT("迟深(m)"));	//
	m_Grid.SetItemText(0, 2, TEXT("TG(%)"));	//
	m_Grid.SetItemText(0, 3, TEXT("C1(%)"));	//
	m_Grid.SetItemText(0, 4, TEXT("C2(%)"));	//
	m_Grid.SetItemText(0, 5, TEXT("C3(%)"));	//
	m_Grid.SetItemText(0, 6, TEXT("iC4(%)"));	//
	m_Grid.SetItemText(0, 7, TEXT("nC4(%)"));	//
	m_Grid.SetItemText(0, 8, TEXT("iC5(%)"));	//
	m_Grid.SetItemText(0, 9, TEXT("nC5(%)"));	//
	m_Grid.SetItemText(0, 10, TEXT("CO2(%)"));	//
	m_Grid.SetItemText(0, 11, TEXT("通道"));	//
	m_Grid.SetItemText(0, 12, TEXT("谱图文件名"));	//

	for (int i = 1; i < 100; i++)
	{
		strTemp.Format(_T("%d"), i);
		m_Grid.SetItemText(i, 0, strTemp);
		m_Grid.SetItemData(i, 0, 0);
		if (i % 2 == 0)
		{
			for (int k = 1; k < 13; k++)
				m_Grid.SetItemBkColour(i, k, RGB(167, 167, 211));
		}
		else
		{
			for (int k = 1; k < 13; k++)
				m_Grid.SetItemBkColour(i, k, RGB(185, 255, 255));
		}
	}

	m_Grid.AutoSize();
	m_Grid.SetColumnWidth(0, 100);
	m_Grid.SetColumnWidth(12,230);
	for (int i = 1; i < 13; i++)
		m_Grid.SetColumnWidth(i, 70);
	m_Grid.SetColumnWidth(11, 50);
	CRect rect;
	GetClientRect(rect);
	m_Grid.MoveWindow(rect);
}

void	CResultRecordView::FillGrid()
{
	m_Grid.SetRowCount(6000);		//24小时最大24*60*4=5760
	CString strTemp;
	int nRows = arrChromResult.GetCount();
	for (int nKK = 1; nKK < nRows + 1; nKK++)
	{
		CHROM_RESULT cr = arrChromResult.GetAt(nKK - 1);
		CTime tt = CTime(cr.lTime);
		m_Grid.SetItemText(nKK, 0, tt.Format(_T("%m-%d %H:%M:%S")));
		m_Grid.SetItemText(nKK, 12, cr.strFileName);
		strTemp.Format(_T("%.1f"), cr.dLagDepth);//LAGDEPTH
		m_Grid.SetItemText(nKK, 1, strTemp);
		strTemp.Format(_T("%.4f"), cr.dTG);
		m_Grid.SetItemText(nKK, 2, strTemp);
		strTemp.Format(_T("%.4f"), cr.dC1);
		m_Grid.SetItemText(nKK, 3, strTemp);
		strTemp.Format(_T("%.4f"), cr.dC2);
		m_Grid.SetItemText(nKK, 4, strTemp);
		strTemp.Format(_T("%.4f"), cr.dC3);
		m_Grid.SetItemText(nKK, 5, strTemp);
		strTemp.Format(_T("%.4f"), cr.dIC4);
		m_Grid.SetItemText(nKK, 6, strTemp);
		strTemp.Format(_T("%.4f"), cr.dNC4);
		m_Grid.SetItemText(nKK, 7, strTemp);
		strTemp.Format(_T("%.4f"), cr.dIC5);
		m_Grid.SetItemText(nKK, 8, strTemp);
		strTemp.Format(_T("%.4f"), cr.dNC5);
		m_Grid.SetItemText(nKK, 9, strTemp);
		strTemp.Format(_T("%.4f"), cr.dCO2);
		m_Grid.SetItemText(nKK, 10, strTemp);
		if (cr.nCH == 0)
			m_Grid.SetItemText(nKK, 11, _T("A"));
		else
			m_Grid.SetItemText(nKK, 11, _T("B"));
	}
		
	for (int i = 1; i < nRows+1; i++)
	{
		if (i % 2 == 0)
		{
			for (int k = 1; k < 13; k++)
				m_Grid.SetItemBkColour(i, k, RGB(167, 167, 211));
		}
		else
		{
			for (int k = 1; k < 13; k++)
				m_Grid.SetItemBkColour(i, k, RGB(185, 255, 255));
		}
	}

	m_Grid.SetRowCount(nRows+1);
	m_Grid.AutoSize();
	m_Grid.SetColumnWidth(0, 120);
	for(int i=1;i<13;i++)
		m_Grid.SetColumnWidth(i, 70);
	m_Grid.SetColumnWidth(11, 50);
	m_Grid.SetColumnWidth(12, 230);
}

//加载趋势曲线
int	CResultRecordView::LoadHisData()
{
	arrChromResult.RemoveAll();
	CTime tE = CTime(m_tStartTime.GetTime() + m_nTimeSpan * 60);
	CString strSQL;
	strSQL.Format(_T("select CREATETIME,FILENAME,LAGDEPTH,TG,C1,C2,C3,IC4,NC4,IC5,NC5,CO2,CHANNEL FROM CHROM_REC where CREATETIME between #%s# and #%s# order by CREATETIME asc"), m_tStartTime.Format(_T("%Y-%m-%d %H:%M:%S")), tE.Format(_T("%Y-%m-%d %H:%M:%S")));

	_variant_t sFldValue;
	_RecordsetPtr pRecordset;
	COleDateTime tt;
	int nKK = 1;
	CString strTemp;
	pRecordset.CreateInstance(_uuidof(Recordset));
	try
	{
		pRecordset->Open((_variant_t)strSQL, m_pCnnWell.GetInterfacePtr(), adOpenForwardOnly, adLockReadOnly, adCmdText);
		while (!pRecordset->adoEOF)
		{
			CHROM_RESULT cr;
			sFldValue = pRecordset->GetCollect(long(0));
			tt = sFldValue.date;
			CTime time(tt.GetYear(), tt.GetMonth(), tt.GetDay(), tt.GetHour(), tt.GetMinute(), tt.GetSecond());
			cr.lTime = time.GetTime();
			sFldValue = pRecordset->GetCollect(long(1));
			cr.strFileName= CString(sFldValue.bstrVal);
			sFldValue = pRecordset->GetCollect(long(2));		//LAGDEPTH
			cr.dLagDepth= sFldValue.fltVal;
			sFldValue = pRecordset->GetCollect(long(3));		//TG
			cr.dTG= sFldValue.fltVal;
			sFldValue = pRecordset->GetCollect(long(4));		//C1
			cr.dC1 =sFldValue.fltVal;
			sFldValue = pRecordset->GetCollect(long(5));		//C2
			cr.dC2=sFldValue.fltVal;
			sFldValue = pRecordset->GetCollect(long(6));		//C3
			cr.dC3=sFldValue.fltVal;
			sFldValue = pRecordset->GetCollect(long(7));		//IC4
			cr.dIC4=sFldValue.fltVal;
			sFldValue = pRecordset->GetCollect(long(8));		//NC4
			cr.dNC4= sFldValue.fltVal;
			sFldValue = pRecordset->GetCollect(long(9));		//IC5
			cr.dIC5=sFldValue.fltVal;
			sFldValue = pRecordset->GetCollect(long(10));		//NC5
			cr.dNC5=sFldValue.fltVal;
			sFldValue = pRecordset->GetCollect(long(11));		//CO2
			cr.dCO2 = sFldValue.fltVal;
			sFldValue = pRecordset->GetCollect(long(12));		//CH
			strTemp=sFldValue.bstrVal;
			strTemp.Trim();
			if (strTemp == _T("A"))
				cr.nCH = 0;
			else
				cr.nCH = 1;
			nKK++;
			arrChromResult.Add(cr);
			pRecordset->MoveNext();
		}
		pRecordset->Close();
	}
	catch (...)
	{
	}
	return nKK-1;
}

//按日期
void CResultRecordView::OnBnClickedBtdate()
{
	// TODO: 在此添加控件通知处理程序代码
	long lBegin, lEnd;
	gl.GetDbTimeInfo(lBegin, lEnd);
	m_tDbStartTime = CTime(lBegin);
	m_tDbEndTime= CTime(lEnd);

	CSelDateDlg dlg;
	dlg.m_tDbStartTime = m_tDbStartTime;
	dlg.m_tDbEndTime = m_tDbEndTime;
	if (dlg.DoModal() == IDOK)
	{
		m_tStartTime = dlg.m_tSel;
		UpdateView();
	}
}

//返回当前时间
void CResultRecordView::OnBnClickedBtnow()
{
	// TODO: 在此添加控件通知处理程序代码
	long lBegin, lEnd;
	gl.GetDbTimeInfo(lBegin, lEnd);
	m_tDbStartTime = CTime(lBegin);
	m_tDbEndTime = CTime(lEnd);

	m_tStartTime = lEnd - m_nTimeSpan * 60;
	UpdateView();
	//若是表格时，移动最后一行
	if (m_nCurMode == 0)	//表格模式
	{
		m_Grid.SetScrollPos(1, 100 * m_Grid.GetRowCount(), TRUE);
	}
}

//表格显示
void CResultRecordView::OnBnClickedBtgrid()
{
	// TODO: 在此添加控件通知处理程序代码
	m_nCurMode = 0;
	m_Grid.ShowWindow(SW_SHOW);
	m_cmbVS.ShowWindow(SW_HIDE);
	m_cmbHS.ShowWindow(SW_HIDE);
	FillGrid();
	Invalidate();
}

//趋势显示
void CResultRecordView::OnBnClickedBttrend()
{
	// TODO: 在此添加控件通知处理程序代码
	m_nCurMode = 1;
	m_Grid.ShowWindow(SW_HIDE);
	m_cmbVS.ShowWindow(SW_SHOW);
	m_cmbHS.ShowWindow(SW_SHOW);
	Invalidate();
}

//选择发生更改
void CResultRecordView::OnCbnSelchangeCmbvs()
{
	// TODO: 在此添加控件通知处理程序代码
	m_nVSType = m_cmbVS.GetCurSel();
	Invalidate();
}

//选择发生更改
void CResultRecordView::OnCbnSelchangeCmbhs()
{
	// TODO: 在此添加控件通知处理程序代码
	m_nHSType = m_cmbHS.GetCurSel();
	switch(m_nHSType)
	{
		case 0:
			m_nTimeSpan= 24 * 60;
			break;
		case 1:
			m_nTimeSpan = 12 * 60;
			break;
		case 2:
			m_nTimeSpan = 6 * 60;
			break;
		case 3:
			m_nTimeSpan = 2 * 60;
			break;
		case 4:
			m_nTimeSpan = 1 * 60;
			break;
	}

	Invalidate();
}


void CResultRecordView::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (m_nCurMode == 1)
	{
		//是否是显示隐藏曲线
		for (int i = 0; i < 9; i++)
		{
			if (m_rtCheck[i].PtInRect(point))
			{
				if (gl.gcMachine.nComShow[i] == 1)
					gl.gcMachine.nComShow[i] = 0;
				else
					gl.gcMachine.nComShow[i] = 1;
				InvalidateRect(m_rtCheck[i]);
				InvalidateRect(m_rtGraph);
				return;
			}
		}
		//曲线中某个时间点数据
		if (m_rtGraph.PtInRect(point))
		{
			int nTime = m_tStartTime.GetTime()+ (point.x - m_rtGraph.left)*(m_nXScale * 60) / m_rtGraph.Width();
			int nCount = arrChromResult.GetCount();
			for (int k = 0; k < nCount; k++)
			{
				CHROM_RESULT cr = arrChromResult.GetAt(k);
				if (cr.lTime >= nTime)
				{
					Chrom_File cf =gl.ReadChmDate(cr.strFileName);
					m_wndHisCurve.UpdateGraph(cf, cr.nCH);
					break;
				}
			}
		}
	}

	CFormView::OnLButtonUp(nFlags, point);
}

void CResultRecordView::OnGridClick(NMHDR *pNotifyStruct, LRESULT* /*pResult*/)
{
	NM_GRIDVIEW* pItem = (NM_GRIDVIEW*)pNotifyStruct;
	CString strTemp;
	//strTemp.Format(_T("Clicked on row %d, col %d\n"), pItem->iRow, pItem->iColumn);
	strTemp = m_Grid.GetItemText(pItem->iRow, 12);
	strTemp.Trim();
	//AfxMessageBox(strTemp);
	Chrom_File cf = gl.ReadChmDate(strTemp);
	strTemp = m_Grid.GetItemText(pItem->iRow, 11);
	strTemp.Trim();
	if(strTemp==_T("A"))
		m_wndHisCurve.UpdateGraph(cf, 0);
	else
		m_wndHisCurve.UpdateGraph(cf, 1);
}

//更新界面
void	CResultRecordView::UpdateView()
{
	LoadHisData();
	if (m_nCurMode == 0)	//表格模式
		FillGrid();
	else					//图形模式
		Invalidate();
}

void CResultRecordView::OnBnClickedBttop()
{
	// TODO: 在此添加控件通知处理程序代码
	long lBegin, lEnd;
	gl.GetDbTimeInfo(lBegin, lEnd);
	m_tDbStartTime = CTime(lBegin);
	m_tDbEndTime = CTime(lEnd);

	m_tStartTime = m_tDbStartTime;
	UpdateView();
}


void CResultRecordView::OnBnClickedBtbottom()
{
	// TODO: 在此添加控件通知处理程序代码
	long lBegin, lEnd;
	gl.GetDbTimeInfo(lBegin, lEnd);
	m_tDbStartTime = CTime(lBegin);
	m_tDbEndTime = CTime(lEnd);

	m_tStartTime = CTime(m_tDbEndTime.GetTime() - m_nTimeSpan * 60);
	UpdateView();
}


void CResultRecordView::OnBnClickedBtup()
{
	// TODO: 在此添加控件通知处理程序代码
	m_tStartTime= CTime(m_tStartTime.GetTime() - m_nTimeSpan * 60);
	if (m_tStartTime < m_tDbStartTime) m_tStartTime = m_tDbStartTime;
	UpdateView();
}


void CResultRecordView::OnBnClickedBtdown()
{
	// TODO: 在此添加控件通知处理程序代码
	m_tStartTime = CTime(m_tStartTime.GetTime() +m_nTimeSpan * 60);
	if (m_tStartTime > m_tDbEndTime) m_tStartTime = CTime(m_tDbEndTime.GetTime() - m_nTimeSpan * 60);
	UpdateView();
}
